#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
int size_numbers_entered;


void display(int a[])
{

	int i = 0; 

	printf("[");

	for (i = 0; i < size_numbers_entered; i++)
		printf("%d ", a[i]);

	printf("] \n");
	
	
	
	
		
}


void check_value_in_list(int a[])
{
	int i, x;

	

	printf("Input the number, for which you want to check in the list \t");
	scanf("%d", &x);
	for (i = 0; i < size_numbers_entered; i++) ;
	{
		if (a[i] == x) {
			printf(" The number you entered exists in the list\n ");
			break;			
}
		else printf("The number does not exist in the list \n");
	}
	
	
}



void reverse(int a[])
{
	int i;

	printf("The original list printed \n");
	display(a);
	printf(" \n \nThe list printed in reverse order: \n ");
	printf("[");
	for (i = size_numbers_entered - 1; i >= 0; i--) {
		printf(" %d", a[i]);
	}
	printf("]");
}



void insert(int a[])
{
    int x,k,l;

    printf("Enter the number you want to insert and the index with a comma in between \n ");
    scanf("%d,%d",&x,&l);
   
   
     
    int b[100],i,j ;
    for(i=0;i<size_numbers_entered;i++)
    {
        b[i]=a[i];
    }
    a[l]=x;
    size_numbers_entered++;
    a=realloc(a,sizeof(int)*size_numbers_entered);

    for(j=l+1;j<size_numbers_entered;j++)
    {
        a[j]=b[j-1];
    }
     
    display(a);

}

void indexx(int a[])
{
	int x, i, k, index_x;

	printf("Enter the number for which you want the index \n ");
	scanf("%d", &x);

	for (i = 0; i < size_numbers_entered; i++) {
		if (a[i] == x)
		 {  break; }
		
			index_x = i;
	}

	printf(" \n Index of %d is %d \n", x, index_x);

}


void list_size(int a[])
{
	printf(" The size of the list is %d \n ", size_numbers_entered);
}
/*
void count(int a[])
{
	int i, count = 0, x;

	printf("Enter the number whose frequency you want in the list \t ");
	scanf("%d", &x);

	for (i = 0; i < size_numbers_entered; i++) {
		if (a[i] == x)
			count++;
	}

	printf(" The frequency of occurence of  \t %d is \t %d \n ", x, count);
}
*/
void sort(int a[])
{



	int i, j, temp;

	for (i = 0;i<size_numbers_entered;i++)
	{
	
	for (j = 0;j<size_numbers_entered;j++)
	{
		if (a[j] > a[i])
		{
			temp = a[i];
			a[i] = a[j];
			a[j] = temp;
			}
		}
	}
		printf(" The sorted list is : \n ");
		printf("[");
		for (i = 0; i < size_numbers_entered; i++) {
			printf(" %d", a[i]);
		}
		printf("]");
	
}
/*
int pop(int a[])
{
	int remove_x, k, index, i, return_value;

	printf("\nEnter the index of the number to be removed \t");
	scanf("%d", &index);

	return_value = a[index];
	
	
        if(index > size_numbers_entered)
        {  return; }
        
	for (i = index + 1; i < size_numbers_entered; i++) {
		a[i - 1] = a[i];
	}
	printf("You popped %d \n", return_value);
	printf("Your list is now\n");
	size_numbers_entered--;
	
	
	
	display(a);
        return return_value;
}


void removefunc(int a[])
{

	int x, remove_x, i, j, k,count;

	printf("\nEnter the number to be removed\t");
	scanf("%d", &x);
	count=0;
	
	remove_x = -1;

	for (i = 0; i < size_numbers_entered; i++) {
		if (a[i] == x) {
			remove_x = i;
                        break;
		}
	}
        if(remove_x!=-1){
	for (j = remove_x; j < size_numbers_entered - 1; j++) {
		a[j] = a[j + 1];
	}
	}
	size_numbers_entered--;
*/	
	display(a);



add_values(int a[])
{
	    int num ,i , number;
	   
	    printf("How many more numbers do you want to enter");
	    scanf("%d", &num);
	    size_numbers_entered+=num;
	    a=realloc(a,sizeof(int)*(size_numbers_entered));
	    
	    for(i=size_numbers_entered -num;i<size_numbers_entered;i++)
	    {
	    
	    printf("Enter a number \t \n");	    
	    scanf("%d", &number);
	    a[i]=number;
	    }
	    printf("Your list is now \t ");
	    display(a);
	    
}
void extend(int a[])
{

	int appendvalue, i, x, number, n;
	int *b;
	
	
	printf(" \n This function extends your current list by concatenating a list or number \t \n");
		add_values(a);
}

void append(int a[])
{
	int i, appendvalue;
	
	printf(" \n Enter value to append \t ");
	scanf("%d", &appendvalue);
	size_numbers_entered++;
	a=realloc(a,sizeof(int)*size_numbers_entered);
	a[size_numbers_entered-1] = appendvalue;
        display(a);
}


/* _____________________________________________________________________________________________________________________*/

/* **************************************************************************************************************** */

int main()
{
	int *a;
	int option=1, number, i=0, counter;
	char string[10], string2[10];
	

	
		printf("\\WELCOME TO PYTHON LIST IMPLEMENTATION/    \n");

	
		printf("Enter how many numbers do you want to enter ");
		scanf("%d", &size_numbers_entered);
		
		int a[size_numbers_entered];   
		
	

		for (i = 0; i < size_numbers_entered; i++) {
			printf("Enter an number ");
			scanf("%d", &number);
			a[i] = number;
			
		}
		printf("\t\t\t\tYour list is \n\n");
		display(a);
		printf("\t\t\t\tYour list has  %d elements \n\n",size_numbers_entered);
		
		
		do {
		printf(" Which function do you want to implement - \n ");
		printf(" \n------------------------------------------------------------------------------------------ \n");
		printf("       	           0)        Display\n");
		printf("                   1)        Append\n");
		printf("                   2)        Extend\n");
		printf("                   3)        Pop\n");
		printf("                   4)        Remove\n");
		printf("                   5)        Index\n");
		printf("                   6)        Insert\n");
		printf("                   7)        list_size\n");
		printf("                   8)        Sort\n");
		printf("                   9)        Reverse\n");
		printf("                   10        Count\n");
		printf("                   11)       Check if value in list\n");
		printf("                   12)       Add more values\n");
		printf("                   13)       Exit\n");
		
		printf
		    (" \n    ------------------------------------------------------------------------------------------   \n  ");

		printf("\t\t\t\t Please enter an option \t");
		scanf("%d", &option);
	

		switch (option) {
		case 0:
			{
			display(a);
		
			}
			break;
		case 1:
			append(a);
			break;
		case 2:
			extend(a);
			break;
		case 3:
			pop(a);
			break;
		case 4:
			
			removefunc(a);
			break;
		case 5:
			indexx(a);
			break;
		case 6:
			insert(a);
			break;
		case 7:
			list_size(a);
			break;
		case 8:
			sort(a);
			break;
		case 9:
			reverse(a);
			break;
		case 10:
			count(a);
			break;
		case 11:
			check_value_in_list(a);
			break;
		case 12:
			add_values(a);
			break;
			
		case 13:
			exit(0);

		}
		printf("\nType 'again' to run the program once again \n");
		scanf("%s", string);
	}
	while (strcmp(string, "again") == 0);
	return 0;

}
